<template>
  <div>
    <el-dialog v-model="visible" :show-close="false" width="700">
      <template #header="{ close, titleId, titleClass }">
        <div class="my-header">
          <h4 :id="titleId" :class="titleClass">
            {{ formData.id ? '编辑' : '创建' }}用户
          </h4>
        </div>
      </template>

      <div>
        <el-form :model="formData" label-width="120px" ref="elFormRef">
          <el-form-item label="用户名" prop="user_name">
            <el-input v-model="formData.user_name" size="large" placeholder="请输入用户名" />
          </el-form-item>

          <el-form-item label="密码" prop="pwd">
            <el-input v-model="formData.pwd" size="large" type="password" placeholder="请输入密码" />
          </el-form-item>

          <!-- <el-form-item label="用户类型" prop="classId">
            <Select v-model="formData.classId" :data="userTypeData" />
          </el-form-item> -->

          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="formData.phone" size="large" placeholder="请输入联系电话" />
          </el-form-item>

          <el-form-item label="电子邮箱" prop="email">
            <el-input v-model="formData.email" size="large" placeholder="请输入电子邮箱" />
          </el-form-item>

          <el-form-item label="状态" prop="status">
            <Select v-model="formData.status" :data="userStatusData" />
          </el-form-item>

          <el-form-item label="分销比例" prop="commission_rate">
            <el-input-number v-model="formData.commission_rate"></el-input-number>
          </el-form-item>

          <el-form-item label="备注信息" prop="remarks">
            <el-input v-model="formData.remarks" size="large" type="textarea" placeholder="请输入备注信息" />
          </el-form-item>
        </el-form>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="close">关闭</el-button>

          <el-button type="primary" @click="handleSubmit">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang='ts' setup>
import { ref, watch, onMounted } from 'vue'
import { userRegionAddAndEdit } from '@/api/index'
import { ElMessage } from 'element-plus'
import Select from '@/components/admin/Select.vue'

const props = defineProps({
  userTypeData: {
    type: Object,
    default: () => ([
      {
        label: '全部',
        value: ''
      },
      {
        label: '区域代理',
        value: 1,
      },
      {
        label: '店铺',
        value: 2,
      },
      {
        label: '经销商',
        value: 3,
      },
      {
        label: '普通用户',
        value: 0
      },
    ])
  },

  userStatusData: {
    type: Object,
    default: () => ([
      {
        label: '冻结',
        value: 0,
        type: 'error'
      },
      {
        label: '正常',
        value: 1,
        type: 'success'
      }
    ])
  }
})

const visible = ref(false)

const elFormRef = ref(null)

const open = (config = {}) => {
  Object.assign(formData.value, config.data)

  formData.value.commission_rate = config.data?.userConfigWithOne?.commission_rate * 100 ?? 0

  visible.value = true
}

const close = () => {
  visible.value = false
}

const formData = ref({
  id: '',
  user_name: '',
  pwd: '',
  classId: 3,
  phone: '',
  email: '',
  status: '',
  remarks: '',
  method: 'add',
  commission_rate: ''
})

const handleSubmit = async () => {
  try {
    formData.value.method = formData.value.id ? 'edit' : 'add'

    const res = await userRegionAddAndEdit(formData.value)

    elFormRef.value.resetFields()

    ElMessage({
      message: `用户${formData.value.id ? '编辑' : '创建'}成功!`,
      type: 'success'
    })

    emit('reload')

    close()
  } catch (err) {
    console.log('handleSubmit', err)

    ElMessage({
      message: '添加用户失败!',
      type: 'error'
    })
  }
}

// 暴露方法给父组件
defineExpose({
  open
});

const emit = defineEmits(['reload'])

onMounted(() => {
  // open({})
})
</script>

<style lang='scss' scoped></style>
